<?php

class AuthController extends AppController
{

	public $name = 'Auth';
	public $helpers = array();
	public $uses = array('ClientAdmin', 'Admin');

	public $po_domain = '';

	function beforeFilter()
	{
		parent::beforeFilter();

		$this->po_domain = Inflector::underscore($this->name);
		$this->set('po_domain', $this->po_domain);
	}

	function profile()
	{
        Auth::Access('admin');
		$po_domain = $this->po_domain;

		$this->pageTitle = strip_tags(__d($po_domain, 'profile_title', true));

	    /*if (Auth::o()->is('client_admin')) {

	        $this->_client_admin_profile();

	    } else*/if (Auth::o()->is('admin')) {

	        $this->_admin_profile();
	    }

	    $this->set('mode', 'edit');
	}

	protected function _admin_profile()
	{
		$po_domain = $this->po_domain;
        $modelName = 'Admin';
        if (! class_exists($modelName)) {
            loadModel($modelName);
        }
        $model =& new $modelName();
	    $PKName    = $model->primaryKey;
        $this->set('modelName', $modelName);
        $this->set('PKName', $PKName);


        $id = Auth::o()->realId();

	    $model->rel();
        $tmp = $model->read(null, $id);

		if (! is_array($tmp) || !count($tmp)) {
			$this->msgError(__d(null, 'There was an internal error.', true).' '.__d(null, 'Try later.', true), @$error_code);
			$this->redirect("/", null, true);
			exit();
		}

		$submit = !empty($this->data);
		if (! $model->allowUpdate($tmp[$modelName])) {
		    $this->data = array($modelName => array());
		}
		foreach ($tmp as $_modelName => $_modelData) {
            if ($_modelName !== $modelName) {
    			$this->data[$_modelName] = $tmp[$_modelName];
            } else {
        		$this->data[$modelName] = am($tmp[$modelName], $this->data[$modelName]);
            }
		}

		if ($model->allowUpdate($tmp[$modelName]) && $submit) {
			$error_msg = '';
			$success_msg = __d($po_domain, 'profile_save', true);
            if (empty($error_msg)) {
				if ($model->save($this->data)) {

				} elseif (!empty($model->validationErrors)) {
					$error_msg = __d(null, 'Data are incorrectly entered.', true).' '.__d(null, 'Try again.', true);
				} else {
	    		    $error_msg = __d(null, 'There was an internal error.', true).' '.__d(null, 'Try later.', true);
				}
            }
			if (empty($error_msg)) {
			    $this->msgNotice($success_msg);
			    $this->redirect("/profile/", null, true);
			    exit();
			} else {
			    $this->msgError($error_msg, @$error_code);
			}
		}
		$this->data[$modelName]['password'] = '';
		$this->data[$modelName]['confirm_password'] = '';
		$this->set('statuses_list', $model->getStatusesList());

		$this->data[$modelName]['__allows']['update'] = $this->{$modelName}->allowUpdate($tmp[$modelName]);
		$this->data[$modelName]['__allows']['delete'] = $this->{$modelName}->allowDelete($tmp[$modelName]);
	}

	function logout()
	{
	    Auth::logout();
        $this->redirect('/login');
        exit();
	}

	function login()
	{
        Auth::Access('*');
        if (Auth::o()->is('guest')) {
    		$po_domain = $this->po_domain;

            $exchange_user_flag = Auth::isLogin();
    	    $this->pageTitle = strip_tags(__d($po_domain, $exchange_user_flag ? 'exchange_user_title' : 'login_title', true));

    	    $this->set('exchange_user_flag', $exchange_user_flag);

    	    $submit = !empty($this->data);
    	    if ($submit) {
        		$data = isset($this->data['none']) ? $this->data['none'] : array();
        		$username = isset($data['username']) ? $data['username'] : '';
        		$password = isset($data['password']) ? $data['password'] : '';

                $modelName = 'ClientAdmin';
                if (! class_exists($modelName)) {
                    loadModel($modelName);
                }
                $model =& new $modelName();
                $user =& $model->searcheUser($username, $password);
                if (! isset($user)) {
        		    try {
                        set_response(200, 'Wrong login data. Login: '.__d(null, 'open_q1', true).$username.__d(null, 'close_q1', true).'.');
        		    } catch (Exception $e) {}

        		    $this->msgError(__d($po_domain, 'wrong_log_data', true), @$error_code);
                } else {
                    Auth::login($user);

                    $this->redirect('/');
                    exit();
                }
                $this->data['none']['password'] = '';
    	    }
        } else {
            $this->render('forbidden');
        }
	}

}

?>